package org.jaaksi.customviewdemo.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

// 删除文章
class XmlUtils {
    public static void main(String[] args) {
        singleXMLStringOperation();
    }

    private static void singleXMLStringOperation() {
        // 139,140,141,121,122,123,124,125,126,127,128,146,147,148,149,150,151,152,153,154,157,159,158,161,162,160,145,144,143,142,163,164,155,156
        ArrayList<String> deleteNameList = new ArrayList<>();
        deleteNameList.add("<string name=\"knowledge_content_69\">");
        deleteNameList.add("<string name=\"knowledge_content_70\">");
        deleteNameList.add("<string name=\"knowledge_content_71\">");
        deleteNameList.add("<string name=\"knowledge_content_72\">");
        deleteNameList.add("<string name=\"knowledge_content_73\">");
        deleteNameList.add("<string name=\"knowledge_content_74\">");
        deleteNameList.add("<string name=\"knowledge_content_75\">");
        deleteNameList.add("<string name=\"knowledge_content_76\">");
        deleteNameList.add("<string name=\"knowledge_content_77\">");
        deleteNameList.add("<string name=\"knowledge_content_78\">");
        deleteNameList.add("<string name=\"knowledge_content_79\">");
        deleteNameList.add("<string name=\"knowledge_content_80\">");
        deleteNameList.add("<string name=\"knowledge_content_81\">");
        deleteNameList.add("<string name=\"knowledge_content_82\">");
        deleteNameList.add("<string name=\"knowledge_content_121\">");
        deleteNameList.add("<string name=\"knowledge_content_122\">");
        deleteNameList.add("<string name=\"knowledge_content_123\">");
        deleteNameList.add("<string name=\"knowledge_content_124\">");
        deleteNameList.add("<string name=\"knowledge_content_125\">");
        deleteNameList.add("<string name=\"knowledge_content_126\">");
        deleteNameList.add("<string name=\"knowledge_content_127\">");
        deleteNameList.add("<string name=\"knowledge_content_128\">");
        deleteNameList.add("<string name=\"knowledge_content_129\">");
        deleteNameList.add("<string name=\"knowledge_content_130\">");
        deleteNameList.add("<string name=\"knowledge_content_131\">");
        deleteNameList.add("<string name=\"knowledge_content_132\">");
        deleteNameList.add("<string name=\"knowledge_content_133\">");
        deleteNameList.add("<string name=\"knowledge_content_134\">");
        deleteNameList.add("<string name=\"knowledge_content_135\">");
        deleteNameList.add("<string name=\"knowledge_content_136\">");
        deleteNameList.add("<string name=\"knowledge_content_137\">");
        deleteNameList.add("<string name=\"knowledge_content_138\">");
        deleteNameList.add("<string name=\"knowledge_content_139\">");
        deleteNameList.add("<string name=\"knowledge_content_140\">");
        deleteNameList.add("<string name=\"knowledge_content_141\">");
//        copySpecifyXmlFileStringToAnotherDir("E:\\google_version\\BP\\BloodPressure\\app\\src\\main\\res","E:\\google_version\\BloodSugar\\app\\src\\main\\res", deleteNameList);
        deleteUnusedString("/Users/yang/Projects/drojian/HeartRate/app/src/main/res", deleteNameList);
    }

    private static void deleteUnusedString(String path, ArrayList<String> deleteNameList) {

        File file = new File(path);
        for (String name : file.list()) {
            System.out.println("name = " + name);
            if (name.equals(".DS_Store")) continue;
            for (String stringXML : new File(path + File.separator + name).list()) {
                if (stringXML.equals("strings.xml")) {
                    File stringFile = new File(path + File.separator + name + File.separator + stringXML);
                    try {
                        FileInputStream inputStream = new FileInputStream(stringFile);
                        InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
                        StringBuffer sbDownload = new StringBuffer();
                        while (reader.ready()) {
                            sbDownload.append((char) reader.read());
                        }
                        reader.close();
                        inputStream.close();

                        StringBuilder sbResult = new StringBuilder();
                        FileOutputStream outputStream = new FileOutputStream(stringFile);
                        OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
                        String[] localStrings = sbDownload.toString().split("\n");
                        for (String str : localStrings) {
                            if (localStrings[localStrings.length - 1].equals(str)) {
                                break;
                            }
                            boolean canAdd = true;
                            for (String deleteStr : deleteNameList) {
//                                if (str.contains(deleteStr)) {
//                                    canAdd = false;
//                                }
                                if (str.contains("<string name=\"knowledge_content_")){
                                    canAdd = false;
                                }
                            }
                            if (canAdd) {
                                sbResult.append(str).append("\n");
                            }
                        }

                        sbResult.append("</resources>");
                        writer.write(sbResult.toString());
                        writer.close();
                        outputStream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}